package com.frxuexi.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.net.ApiService
import com.frxuexi.lib_base.net.VideoApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel  @Inject constructor(val apiService: ApiService):BaseViewModel() {

    val addParkSuc = MutableLiveData<ParkBeanItem>()

    val getParkByIdData =MutableLiveData<ParkBeanItem>()
    val updateParkData =MutableLiveData<ParkBeanItem>()

    val deleteParkData =MutableLiveData<Boolean>()

    fun getParkByType(para:String,type:String):Flow<PagingData<ParkBeanItem>>{
        return Pager(config = PagingConfig(10), pagingSourceFactory = {
            ParkDataSource(apiService,para, type)
        }).flow.cachedIn(viewModelScope)
    }

    fun addPark(item: ParkBeanItem){
        httpRequest({apiService.addPark(toBody(item))},addParkSuc::postValue)
    }

    fun getParkById(id:Int){
        httpRequest({apiService.getParkById(id)},getParkByIdData::postValue)
    }

    fun updatePark(id:Int,item:ParkBeanItem){
        httpRequest({apiService.updatePark(id,toBody(item))},updateParkData::postValue)
    }

    fun deletePark(id:Int){
        httpRequest({apiService.deletePark(id)},deleteParkData::postValue)
    }
}